/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-9         luwei
 */

package com.coocaa.launcher.guide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

import com.tianci.appstore.R;

public class PageNum extends View {

    private Context context = null;
    private int pageCount = 0;
    private int currentPage = 0;
    private Bitmap mCurrentBitmap = null;
    private Bitmap mOtherBitmap = null;
    public static float mWidth = 0f;
    public static final int space = 10;

    public PageNum(Context context, AttributeSet attrs) {
        super(context, attrs);
        loadResources();
    }

    public PageNum(Context context) {
        super(context);
        this.context = context;
        loadResources();
    }

    public void updatePageNum(int count, int current) {
        pageCount = count;
        currentPage = current;
        invalidate();
    }

    private void loadResources() {
        mCurrentBitmap = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.pic_point_focus);
        mWidth = mCurrentBitmap.getWidth();
        mOtherBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.pic_point);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float temp = getStartXPos();
        for (int i = 0; i < pageCount; i++) {
            if (i != currentPage) {
                canvas.drawBitmap(mOtherBitmap, temp, 0, null);
            } else {
                canvas.drawBitmap(mCurrentBitmap, temp, 0, null);
            }
            temp = temp + space + mWidth;
        }
    }

    private int getStartXPos() {
        return (1920 - (pageCount * mOtherBitmap.getWidth() + (pageCount - 1) * space)) / 2;
    }

    public int getPageCount() {
        return pageCount;
    }
}
